import api from "../../utils/apiInstance"
import {groupBy} from '../../utils/util'
const app = getApp();
Page({  
  data: {
    baseUrl: app.globalData.baseUrl,
    fileUrl: app.globalData.fileUrl,
    tabListConst:[
      {name:'全部', tag:"" , params:{status:'all'}},
      {name:'使用中', tag:"", params:{status:'Normal'}},
      {name:'快过期', tag:"", params:{status:'ExpireImmediately'}},
      {name:'已过期', tag:"" , params:{status:'Expired|Cancel'}},
      {name:'审核', tag:"", params:{status:'PendingReview|NotPassReview'}}
    ],
    tabList:[],
    tabIndex:0,
    tabStatus:"all",
    cardList:{},//对象列表
    showCardList:[] //显示列表
  },
  async onShow(){
    
    this.loadData()
  },
  async loadData(){

    const cardList = await api.get(`/v1/openApi/certificate/recheck/search/${wx.getStorageSync('userinfo').siocSapNo}`);
    const tempArray = groupBy(cardList.rows,item=>item.status)
    let  tempCardList = {};
    //构造tab
    const tempTabList = [{name:'全部', tag:`[${cardList.rows.length}]`, params:{status:'all'}}];
    tempCardList['all'] =cardList.rows
    for(let i in tempArray){
      const tabTarget = this.data.tabListConst.find(item=>{
        return item.params.status.split("|").includes(i)
      })
      tempTabList.push({
        name:tabTarget.name,
        tag:`[${tempArray[i].length}]`,
        params:tabTarget.params
      })
    }
    tempCardList = {...tempCardList, ...tempArray}
    
    this.setData({
      tabList:tempTabList,
      cardList:tempCardList,
      showCardList:tempCardList['all']
    })

    console.log("tempCardList",tempCardList);
  },

  //审核
  doShenHe(params){
    wx.navigateTo({
      url: `/subPages/zhengJianGlForm/zhengJianGlForm?id=${params.detail.id}&type=4`,
    })
  },


  tabChange(e){
    let tempArray = [];
    if(e.detail.params.status){

      if(e.detail.params.status.indexOf("|")>0){

     
        let statusList = e.detail.params.status.split('|')
        console.log("statusList",statusList)
        statusList.forEach(item => {
            this.data.cardList[item]&&tempArray.push(...this.data.cardList[item])
        });
        
      }
      else{
        tempArray = this.data.cardList[e.detail.params.status]
      }
    }

    this.setData({
      tabIndex:e.detail.tabIndex,
      tabStatus:e.detail.params.status,
      showCardList:tempArray
    })
    console.log('e',e);
    
  },


})
